# Contributor: Frederic Wang <fred.wang@free.fr>

_realname=pbr
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.0.4
pkgrel=1
pkgdesc="Python Build Reasonableness (mingw-w64)"
url="https://launchpad.net/pbr"
license=("Apache License, Version 2.0")
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-six"
             "${MINGW_PACKAGE_PREFIX}-python3-six")
#_dtoken="d5/d6/f2bf137d71e4f213b575faa9eb426a8775732432edb67588a8ee836ecb80"
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/openstack-dev/pbr/archive/${pkgver}.tar.gz")
sha256sums=('df8f500937a0f7f1d2172a7d0b66e0cd1343538bffeac05f818633a1a355b582')

prepare() {
  cd ${srcdir}
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH}| true
    cp -r "${_realname}-${pkgver}" "python${pver}-build-${CARCH}"
  done
}

package_python3-pbr() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-six")
  install=${_realname}3-${CARCH}.install

  cd ${srcdir}/python3-build-${CARCH}
  ${MINGW_PREFIX}/bin/python3 setup.py build
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}

package_python2-pbr() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-six")
  install=${_realname}2-${CARCH}.install

  cd ${srcdir}/python2-build-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py build
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done

  rename -v "pbr" "pbr2" "${pkgdir}${MINGW_PREFIX}"/bin/*
}

package_mingw-w64-i686-python2-pbr() {
  package_python2-pbr
}

package_mingw-w64-i686-python3-pbr() {
  package_python3-pbr
}

package_mingw-w64-x86_64-python2-pbr() {
  package_python2-pbr
}

package_mingw-w64-x86_64-python3-pbr() {
  package_python3-pbr
}
